package com.ljfiction.book.tools;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by liao on 2017/12/27.
 */

public class KeyBoardHelper {
    private int blankHeight = 0;
    private Context mContext;
    private View mMoveLayout;//需要移动的布局
    private View mButton;//不被遮挡的最后一个view
    private View content;

    private int screenHeight = 0;//屏幕高度
    private int bottomHeight = 0;

    private KeyBoardHelper(Context mContext) {
        this.mContext = mContext;
        ((Activity) mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        screenHeight = mContext.getResources().getDisplayMetrics().heightPixels;
        content = ((Activity) mContext).findViewById(android.R.id.content);
        content.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);

    }

    public static KeyBoardHelper create(Context mContext) {
        return new KeyBoardHelper(mContext);
    }

    public void bindMoveView(View mMoveLayout) {
        this.mMoveLayout = mMoveLayout;

    }

    public void bindBottomButton(final View mButton) {
        this.mButton = mButton;
        if (mButton == null) {
            return;
        }
        mButton.post(new Runnable() {
            @Override
            public void run() {
                DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
                int[] location = new int[2];
                mButton.getLocationOnScreen(location);
                int y = location[1];
                int height = displayMetrics.heightPixels;
                bottomHeight = height - (y + mButton.getHeight());
            }
        });
    }


    public void destroy() {
        content.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
    }

    private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            //获取键盘的高度
            Rect rect = new Rect();
            ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int newBlankHeight = screenHeight - rect.bottom;
            if (mMoveLayout == null) {
                return;
            }
            final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mMoveLayout
                    .getLayoutParams();
            if (newBlankHeight != blankHeight) {
                if (newBlankHeight > blankHeight) {
                    if (bottomHeight < newBlankHeight) {

                        int offset = bottomHeight - newBlankHeight;
                        lp.topMargin = offset-10;
                        mMoveLayout.setLayoutParams(lp);
                    }
                } else {
                    //键盘关闭
                    if (lp.topMargin != 0) {
                        lp.topMargin = 0;
                        mMoveLayout.setLayoutParams(lp);
                    }
                }
            }
            blankHeight = newBlankHeight;
        }
    };
}
